#include "master.h"
#include <shlwapi.h>
#include "comfunct/comfunct.h"

void RegisterExtensions(LPTSTR szCommand, size_t s_tCommandLength)
{
	const TCHAR szExtensions[4][7] = {TEXT(".asxml"), TEXT(".waxml"), TEXT(".wmxml"), TEXT(".wvxml")};
	HKEY hRootExtensions[4] = {NULL};
	HKEY hShell = NULL, hPlay = NULL, hCommand = NULL;
	int i = 0;

	for(; i < 4; ++i)
	{
		RegCreateKeyEx(HKEY_CLASSES_ROOT, szExtensions[i], 0, NULL, REG_OPTION_NON_VOLATILE,
					   KEY_WRITE, NULL, &hRootExtensions[i], NULL);
		RegSetValueEx(hRootExtensions[i], NULL, 0, REG_SZ, (const BYTE*)TEXT("AudioPlayer.XMLPlaylist"), 24 * sizeof(TCHAR));
		RegSetValueEx(hRootExtensions[i], TEXT("Content Type"), 0, REG_SZ, (const BYTE*)TEXT("text/xml"), 9 * sizeof(TCHAR));
		RegSetValueEx(hRootExtensions[i], TEXT("PerceivedType"), 0, REG_SZ, (const BYTE*)TEXT("xml"), 4 * sizeof(TCHAR));
		RegCreateKeyEx(hRootExtensions[i], TEXT("shell"), 0, NULL, REG_OPTION_NON_VOLATILE,
					   KEY_WRITE, NULL, &hCommand, NULL);
		RegCreateKeyEx(hCommand, TEXT("play"), 0, NULL, REG_OPTION_NON_VOLATILE,
					   KEY_WRITE, NULL, &hPlay, NULL);
		RegCreateKeyEx(hPlay, TEXT("command"), 0, NULL, REG_OPTION_NON_VOLATILE,
					   KEY_WRITE, NULL, &hCommand, NULL);
		RegSetValueEx(hCommand, NULL, 0, REG_SZ, (const BYTE*)szCommand, (DWORD)s_tCommandLength);
		RegCloseKey(hCommand);
		RegCloseKey(hPlay);
		RegCloseKey(hShell);
		RegCloseKey(hRootExtensions[i]);			
	}
	return;
}

void RegisterPlaylistFormat(HWND hwnd)
{
	HKEY hPlaylistInfo = NULL, hDefaultIcon = NULL, hShell = NULL, hPlay = NULL, hCommand = NULL;
	const TCHAR* szDescription = GetLocalText(153);
	TCHAR szAudioPlayerPath[MAX_PATH] = {0};
	TCHAR* szIconPath = NULL, *szCommand = NULL;
	size_t s_tIconPathLength = 0, s_tCommandLength = 0;
	size_t s_tDescLength = ((_tcslen(szDescription) + sizeof(TCHAR)) * sizeof(TCHAR));
	HMENU hMenu = GetMenu(hwnd), hToolsMenu = GetSubMenu(hMenu, TOOLS_MENU);

	RegCreateKeyEx(HKEY_CLASSES_ROOT, TEXT("AudioPlayer.XMLPlaylist"), 0, NULL, REG_OPTION_NON_VOLATILE,
				   KEY_WRITE, NULL, &hPlaylistInfo, NULL);
	RegSetValueEx(hPlaylistInfo, NULL, 0, REG_SZ, (BYTE*)szDescription, (DWORD)s_tDescLength);
	RegSetValueEx(hPlaylistInfo, TEXT("FriendlyTypeName"), 0, REG_SZ, (BYTE*)szDescription, (DWORD)s_tDescLength);

	RegCreateKeyEx(hPlaylistInfo, TEXT("DefaultIcon"), 0, NULL, REG_OPTION_NON_VOLATILE,
				   KEY_WRITE, NULL, &hDefaultIcon, NULL);

	GetModuleFileName(NULL, szAudioPlayerPath, MAX_PATH);

	s_tIconPathLength = (_tcslen(szAudioPlayerPath) + 3) * sizeof(TCHAR);

	szIconPath = malloc(s_tIconPathLength);
	_tcscpy(szIconPath, szAudioPlayerPath);
	_tcscat(szIconPath, TEXT(",0"));

	RegSetValueEx(hDefaultIcon, NULL, 0, REG_SZ, (const BYTE*)szIconPath, (DWORD)s_tIconPathLength);

	free(szIconPath);
	RegCloseKey(hDefaultIcon);

	RegCreateKeyEx(hPlaylistInfo, TEXT("shell"), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hShell, NULL);
	RegSetValueEx(hShell, NULL, 0, REG_SZ, (const BYTE*)TEXT("Play"), 5 * sizeof(TCHAR));
	RegCreateKeyEx(hShell, TEXT("play"), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hPlay, NULL);
	RegCreateKeyEx(hPlay, TEXT("command"), 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hCommand, NULL);

	PathQuoteSpaces(szAudioPlayerPath);
	s_tCommandLength = ((_tcslen(szAudioPlayerPath) + 9) * sizeof(TCHAR)); /* We have to add on " -o "%1"" */
	szCommand = malloc(s_tCommandLength);
	_tcscpy(szCommand, szAudioPlayerPath);
	_tcscat(szCommand, TEXT(" -o \"%1\""));

	RegSetValueEx(hCommand, NULL, 0, REG_SZ, (const BYTE*)szCommand, (DWORD)s_tCommandLength);

	RegCloseKey(hCommand);
	RegCloseKey(hPlay);
	RegCloseKey(hShell);
	RegCloseKey(hPlaylistInfo);

	RegisterExtensions(szCommand, s_tCommandLength);

	free(szCommand);

	EnableMenuItem(hToolsMenu, IDI_TOOLS_REGISTER, MF_GRAYED);
	EnableMenuItem(hToolsMenu, IDI_TOOLS_UNREGISTER, MF_ENABLED);

	return;
}

void UnRegisterExtensions(HWND hwnd)
{
	const TCHAR szExtensions[4][7] = {TEXT(".asxml"), TEXT(".waxml"), TEXT(".wmxml"), TEXT(".wvxml")};
	int i = 0;
	HMENU hMenu = GetMenu(hwnd), hToolsMenu = GetSubMenu(hMenu, TOOLS_MENU);
		
	for(; i < 4; ++i)
	{
		if(SHDeleteKey(HKEY_CLASSES_ROOT, szExtensions[i]) != ERROR_SUCCESS)
		{
			ErrorInfo();
			ErrorMessage(GetLocalText(54));
		}
	}
	SHDeleteKey(HKEY_CLASSES_ROOT, TEXT("AudioPlayer.XMLPlaylist"));
	EnableMenuItem(hToolsMenu, IDI_TOOLS_UNREGISTER, MF_GRAYED);
	EnableMenuItem(hToolsMenu, IDI_TOOLS_REGISTER, MF_ENABLED);
	return;
}

void CheckAndFixOpenWith()
{
	HKEY hCommand = NULL;
	TCHAR szAudioPlayerPath[MAX_PATH] = TEXT("");
	DWORD dwExistingLength = MAX_PATH + 3;
	TCHAR szExistingCommand[MAX_PATH + 3] = TEXT("");
	TCHAR* szFixedCommand = NULL;
	size_t s_tFixedCommandLength = 0;

	if(RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("Applications\\AudioPlayer.exe\\shell\\open\\command"), 0, KEY_READ | KEY_WRITE, &hCommand) != ERROR_SUCCESS)
	{
        return;
    }

	RegQueryValueEx(hCommand, NULL, NULL, NULL, (BYTE*)&szExistingCommand, &dwExistingLength);

	if(_tcsstr(szExistingCommand, TEXT("-o")) != NULL)
	{
		RegCloseKey(hCommand);
		return;
	}
    
    GetModuleFileName(NULL, szAudioPlayerPath, MAX_PATH);
	PathQuoteSpaces(szAudioPlayerPath);

	s_tFixedCommandLength = ((_tcslen(szAudioPlayerPath) + 9) * sizeof(TCHAR)); /* We have to add on " -o "%1"" */
	szFixedCommand = malloc(s_tFixedCommandLength);
	_tcscpy(szFixedCommand, szAudioPlayerPath);
	_tcscat(szFixedCommand, TEXT(" -o \"%1\""));
	RegSetValueEx(hCommand, NULL, 0, REG_SZ, (const BYTE*)szFixedCommand, (DWORD)s_tFixedCommandLength);

	free(szFixedCommand);
	RegCloseKey(hCommand);

	return;
}

bool AreTypesRegistered()
{
    HKEY hAudioPlayerPlaylist = NULL;
    LONG lResult = 0L;
    
    lResult = RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("AudioPlayer.XMLPlaylist"), 0, KEY_READ, &hAudioPlayerPlaylist);
    
    if(lResult != ERROR_SUCCESS || hAudioPlayerPlaylist == NULL)
    {
        return false;
    }
    else
    {
        RegCloseKey(hAudioPlayerPlaylist);
        return true;
    }
}
